<template>
  <div id="app">
    <div class="container">
      <div class="form-card">
        <h1>用户注册</h1>
        <form @submit.prevent="registerUser" class="registration-form">
          <div class="form-group">
            <label for="fullName">姓名</label>
            <input 
              type="text" 
              v-model="user.fullName" 
              id="fullName" 
              :class="{'error': errors.fullName}"
              @blur="validateField('fullName')"
              required 
            />
            <span class="error-message" v-if="errors.fullName">{{ errors.fullName }}</span>
          </div>

          <div class="form-group">
            <label for="email">电子邮件</label>
            <input 
              type="email" 
              v-model="user.email" 
              id="email" 
              :class="{'error': errors.email}"
              @blur="validateField('email')"
              required 
            />
            <span class="error-message" v-if="errors.email">{{ errors.email }}</span>
          </div>

          <div class="form-group">
            <label for="password">密码</label>
            <input 
              type="password" 
              v-model="user.password" 
              id="password" 
              :class="{'error': errors.password}"
              @blur="validateField('password')"
              required 
            />
            <span class="error-message" v-if="errors.password">{{ errors.password }}</span>
          </div>

          <div class="form-group">
            <label for="birthday">生日</label>
            <input 
              type="date" 
              v-model="user.birthday" 
              id="birthday" 
              :class="{'error': errors.birthday}"
              @blur="validateField('birthday')"
              required 
            />
            <span class="error-message" v-if="errors.birthday">{{ errors.birthday }}</span>
          </div>

          <div class="form-group radio-group">
            <label>性别</label>
            <div class="radio-options">
              <label class="radio-label">
                <input type="radio" value="Male" v-model="user.gender" />
                <span>男</span>
              </label>
              <label class="radio-label">
                <input type="radio" value="Female" v-model="user.gender" />
                <span>女</span>
              </label>
            </div>
            <span class="error-message" v-if="errors.gender">{{ errors.gender }}</span>
          </div>

          <div class="form-group">
            <label for="profession">职业</label>
            <select 
              v-model="user.profession" 
              id="profession"
              :class="{'error': errors.profession}"
              @blur="validateField('profession')"
            >
              <option value="">请选择职业</option>
              <option value="Developer">开发者</option>
              <option value="Designer">设计师</option>
              <option value="Manager">经理</option>
              <option value="Student">学生</option>
              <option value="Other">其他</option>
            </select>
            <span class="error-message" v-if="errors.profession">{{ errors.profession }}</span>
          </div>

          <div class="form-group checkbox-group">
            <label class="checkbox-label">
              <input type="checkbox" v-model="user.married" id="married" />
              <span>已婚</span>
            </label>
          </div>

          <div class="form-group">
            <label for="note">备注</label>
            <textarea 
              v-model="user.note" 
              id="note"
              rows="4"
              :class="{'error': errors.note}"
              @blur="validateField('note')"
            ></textarea>
            <span class="error-message" v-if="errors.note">{{ errors.note }}</span>
          </div>

          <button type="submit" :disabled="!isFormValid">注册</button>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      user: {
        fullName: '',
        email: '',
        password: '',
        birthday: '',
        gender: '',
        profession: '',
        married: false,
        note: ''
      },
      errors: {
        fullName: '',
        email: '',
        password: '',
        birthday: '',
        gender: '',
        profession: '',
        note: ''
      }
    };
  },
  computed: {
    isFormValid() {
      return !Object.values(this.errors).some(error => error !== '') &&
             this.user.fullName &&
             this.user.email &&
             this.user.password &&
             this.user.birthday &&
             this.user.gender &&
             this.user.profession;
    }
  },
  methods: {
    validateField(field) {
      this.errors[field] = '';
      
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      const today = new Date();
      const birthDate = new Date(this.user.birthday);
      
      switch(field) {
        case 'fullName':
          if (this.user.fullName.length < 2) {
            this.errors.fullName = '姓名至少需要2个字符';
          }
          break;
        case 'email':
          if (!emailRegex.test(this.user.email)) {
            this.errors.email = '请输入有效的电子邮件地址';
          }
          break;
        case 'password':
          if (this.user.password.length < 6) {
            this.errors.password = '密码至少需要6个字符';
          }
          break;
        case 'birthday':
          if (birthDate > today) {
            this.errors.birthday = '生日不能是未来日期';
          }
          break;
        case 'profession':
          if (!this.user.profession) {
            this.errors.profession = '请选择职业';
          }
          break;
        case 'note':
          if (this.user.note.length > 500) {
            this.errors.note = '备注不能超过500个字符';
          }
          break;
      }
    },
    validateForm() {
      ['fullName', 'email', 'password', 'birthday', 'profession'].forEach(field => {
        this.validateField(field);
      });
      if (!this.user.gender) {
        this.errors.gender = '请选择性别';
      }
      return !Object.values(this.errors).some(error => error !== '');
    },
    async registerUser() {
      if (!this.validateForm()) {
        return;
      }

      try {
        const response = await axios.post('http://localhost:8081/api/users/register', this.user);
        alert('注册成功！');
        console.log(response.data);
        this.resetForm();
      } catch (error) {
        alert('注册失败：' + (error.response?.data?.message || '请稍后重试'));
        console.error(error);
      }
    },
    resetForm() {
      this.user = {
        fullName: '',
        email: '',
        password: '',
        birthday: '',
        gender: '',
        profession: '',
        married: false,
        note: ''
      };
      this.errors = {
        fullName: '',
        email: '',
        password: '',
        birthday: '',
        gender: '',
        profession: '',
        note: ''
      };
    }
  }
};
</script>

<style>
.container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.form-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 15px 30px rgba(0,0,0,0.1);
  padding: 40px;
  width: 100%;
  max-width: 600px;
}

h1 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
  font-size: 2em;
}

.registration-form {
  display: grid;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

label {
  color: #2c3e50;
  font-weight: 600;
  font-size: 0.9em;
}

input:not([type="radio"], [type="checkbox"]),
select,
textarea {
  padding: 12px;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 1em;
  transition: all 0.3s ease;
}

input:focus,
select:focus,
textarea:focus {
  border-color: #4299e1;
  outline: none;
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.2);
}

.radio-group,
.checkbox-group {
  margin: 10px 0;
}

.radio-options {
  display: flex;
  gap: 20px;
}

.radio-label,
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

button {
  background: #4299e1;
  color: white;
  padding: 15px 30px;
  border: none;
  border-radius: 8px;
  font-size: 1em;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  width: 100%;
  margin-top: 20px;
}

button:hover:not(:disabled) {
  background: #3182ce;
  transform: translateY(-2px);
}

button:disabled {
  background: #a0aec0;
  cursor: not-allowed;
}

.error {
  border-color: #fc8181 !important;
}

.error-message {
  color: #e53e3e;
  font-size: 0.8em;
  margin-top: 4px;
}

@media (max-width: 768px) {
  .form-card {
    padding: 20px;
  }
  
  .radio-options {
    flex-direction: column;
    gap: 10px;
  }
}
</style>